<?php 


class userClass extends mysqlClass{

	

	#constructor extends parent constructor
	public function __construct($params=array()) {
		parent::__construct($params);
	}


	public function validateEmail($email) {
	  // First, we check that there's one @ symbol, 
	  // and that the lengths are right.
		if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
	    // Email invalid because wrong number of characters 
	    // in one section or wrong number of @ symbols.
			return false;
		}
	  // Split it into sections to make life easier
		$email_array = explode("@", $email);
		$local_array = explode(".", $email_array[0]);
		for ($i = 0; $i < sizeof($local_array); $i++) {
			if
				(!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&
					↪'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$",
			$local_array[$i])) {
					return false;
				}
			}
	  // Check if domain is IP. If not, 
	  // it should be valid domain name
			if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) {
				$domain_array = explode(".", $email_array[1]);
				if (sizeof($domain_array) < 2) {
	        return false; // Not enough parts to domain
	    }
	    for ($i = 0; $i < sizeof($domain_array); $i++) {
	    	if
	    		(!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|
	    			↪([A-Za-z0-9]+))$",
	    	$domain_array[$i])) {
	    			return false;
	    		}
	    	}
	    }
	    return true;
	}

	#login user
	public function log_in($email,$password){

		if($this->validateEmail($email)){
			$password = md5($password);

		#create query
			$result=$this->fetch_result("SELECT id,email FROM user WHERE email='{$email}' AND password='{$password}'");
			

			if(count($result)>0){

			#update last login
				$date = date("Y-m-d H:i:s");

			#create session user id 
				$session_user = md5($result[0]['id'].$date);
				$this->execute_query("UPDATE user SET last_login='{$date}', session_user='{$session_user}' WHERE id = '{$result[0]['id']}'");
				$result[0]['session_user']= $session_user;
			}
			return $result;
		}else{
			return false;
		}

	}
	# get user data by id
	public function get_user_by_id($id){		
		
		return	$this->fetch_result("SELECT * FROM user WHERE id='{$id}'"); 		

	}

		# get user data by session_user
	public function get_user_by_session($session_user){		
		
		return	$this->fetch_result("SELECT * FROM user WHERE session_user='{$session_user}'"); 		

	}


	# recover user password
	public function recover_password($email){		

		#generate random password
		$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
		$random_pass = substr(str_shuffle($chars),0,6);
		$random_pass_encrypt= md5($random_pass);

		#create query for updating password
		$query = "UPDATE user SET password='{$random_pass_encrypt}' WHERE email='{$email}'";

		$this->execute_query($query);

		# send mail to be implemented
	}

	#create user 
	public function create_user($params){

	}

    # parent extende destructor
	public function __destruct() {
        parent::__destruct();
    }

}

?>